package com.zmj.sy.mom.srv.aps.service.scheduler.resource.shift;

public class Range<T extends Comparable<T>> implements Comparable<Range<T>> {

    private T start;

    private Boolean includeStart;

    private T end;

    private Boolean includeEnd;

    public static <T extends Comparable<T>> Range<T> of(T start, T end) {
        return of(start, true, end, true);
    }

    public static <T extends Comparable<T>> Range<T> of(T start, Boolean includeStart, T end, Boolean includeEnd) {
        Range<T> range = new Range<>();
        range.start = start;
        range.includeStart = includeStart;
        range.end = end;
        range.includeEnd = includeEnd;
        return range;
    }

    public static <T extends Comparable<T>> boolean include(T start, T end, T value) {
        return of(start, end).include(value);
    }
    public static <T extends Comparable<T>> boolean include(T start, boolean includeStart, T end, boolean includeEnd, T value) {
        return (start.compareTo(value) < 0 || start.compareTo(value) == 0 && includeStart)
                && (end.compareTo(value) > 0 || end.compareTo(value) == 0 && includeEnd);
    }

    @Override
    public int compareTo(Range<T> o) {
        return this.start.compareTo(o.start);
    }

    public boolean isEmpty() {
        return this.start.compareTo(this.end) > 0
                || (this.start.compareTo(this.end) == 0 && (!this.includeStart || !this.includeEnd));
    }

    public boolean include(T value) {
        return include(this.start, this.includeStart, this.end, this.includeEnd, value);
    }

}
